/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.layout.GroupLayout;
import tools.ExcelExporter;

public class OKlient extends JFrame {

    private String name;

    public OKlient(java.util.List klienci, String name, int nrsp, int nrws, MTWsi mtw) {
        nazwa = (new StringBuilder()).append("Klienci miejscowo\u015Bci ").append(name).toString();
        kl = klienci;
        this.name = name;
        nrSp = nrsp;
        nrWs = nrws;
        mtk = new MTKlientow(kl);
        this.mtw = mtw;
        initComponents();
        jTable1.setAutoCreateRowSorter(true);
    }

    private void initComponents() {
        jPanel1 = new JPanel();
        jScrollPane1 = new JScrollPane();
        jTable1 = new JTable(mtk);
        jButton1 = new JButton();
        jButton2 = new JButton();
        jButton3 = new JButton();
        jButton4 = new JButton();
        jLabel1 = new JLabel(nazwa);
        jButton5 = new JButton();
        jButton6 = new JButton();
        setDefaultCloseOperation(2);
        setTitle("Sp\363\u0142ki Wodne - okno klient\363w");
        setResizable(false);
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jPanel1.setMinimumSize(new Dimension(800, 600));
        jScrollPane1.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent evt) {
                jScrollPane1KeyPressed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jScrollPane1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                jScrollPane1MouseClicked(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jTable1.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jTable1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jButton1.setText("Dodaj");
        jButton1.setMinimumSize(new Dimension(0, 0));
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jButton2.setText("Usu\u0144");
        jButton2.setMinimumSize(new Dimension(0, 0));
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jButton3.setText("Edytuj");
        jButton3.setMinimumSize(new Dimension(0, 0));
        jButton3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jButton4.setText("Otw\363rz");
        jButton4.setMinimumSize(new Dimension(0, 0));
        jButton4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jButton5.setText("Zamknij");
        jButton5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        jButton6.setText("Do Excela");
        jButton6.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
            final OKlient this$0;
            

            {
                this$0 = OKlient.this;
            //super();
            }
        });
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(26, 26, 26).add(jPanel1Layout.createParallelGroup(1).add(jLabel1).add(jPanel1Layout.createSequentialGroup().add(jScrollPane1, -2, 587, -2).add(54, 54, 54).add(jPanel1Layout.createParallelGroup(1, false).add(jButton3, -1, -1, 32767).add(jButton1, -1, -1, 32767).add(jButton4, -1, 92, 32767).add(jButton2, -1, 92, 32767).add(2, jButton5).add(jButton6)))).addContainerGap(41, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(81, 81, 81).add(jButton1, -2, -1, -2).addPreferredGap(0).add(jButton2, -2, -1, -2).addPreferredGap(0).add(jButton3, -2, -1, -2).add(54, 54, 54).add(jButton6).add(jButton4, -2, -1, -2)).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jLabel1).add(26, 26, 26).add(jScrollPane1, -2, 450, -2))).addPreferredGap(0, 60, 32767).add(jButton5).addContainerGap()));
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2));
        pack();
    }

    private void jTable1MouseClicked(MouseEvent evt) {
        if (evt.getClickCount() >= 2 && jTable1.getSelectedRowCount() > 0) {
            openKlient(jTable1.getSelectedRow());
        }
    }

    private void jScrollPane1MouseClicked(MouseEvent evt) {
        if (evt.getClickCount() >= 2 && jTable1.getSelectedRowCount() > 0) {
            openKlient(jTable1.getSelectedRow());
        }
    }

    private void jButton4ActionPerformed(ActionEvent evt) {
        int nr = jTable1.getSelectedRow();
        if (nr >= 0) {
            openKlient(nr);
        }
    }

    private void jButton6ActionPerformed(ActionEvent evt) {
        fc.setSelectedFile(new File("excel" + File.separator + "Miejscowosc_" + name + ".xls"));

        int returnVal = fc.showSaveDialog(this);
        if (returnVal == 0) {
            File file = fc.getSelectedFile();

            try {
                ExcelExporter.exportTable(jTable1, file);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Błąd podczas eksportu tabeli do pliku XLS");
            }
        }

    }

    private void jTable1KeyPressed(KeyEvent evt) {
        label0:
        {
            int key;
            label1:
            {
                key = evt.getKeyCode();
                KeyEvent _tmp = evt;
                if (key != 10) {
                    KeyEvent _tmp1 = evt;
                    if (key != 32) {
                        break label1;
                    }
                }
                openKlient(jTable1.getSelectedRow());
                break label0;
            }
            KeyEvent _tmp2 = evt;
            if (key == 27) {
                dispose();
            }
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            int odp = JOptionPane.showConfirmDialog(this, "Czy jestes pewien, \u017Ce chcesz usun\u0105\u0107 wybranego klienta?", "Potwierdzenie", 0);
            if (odp == 0) {
                mtk.delRow(jTable1.getSelectedRow());
            }
        }
    }

    private void formWindowClosing(WindowEvent evt) {
        zakoncz();
    }

    private void jButton5ActionPerformed(ActionEvent evt) {
        zakoncz();
    }

    private void jButton3ActionPerformed(ActionEvent evt) {
        int nr = jTable1.getSelectedRow();
        if (nr >= 0) {
            openKlient(nr);
        }
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        oek = new OEKlient(new Klient(Integer.valueOf(nrSp), Integer.valueOf(nrWs), Integer.valueOf(kl.size() + 1), "", "", "", "", "", "", Float.valueOf(0.0F)), mtk, true);
        oek.setVisible(true);
    }

    private void jScrollPane1KeyPressed(KeyEvent evt) {
        label0:
        {
            int key;
            label1:
            {
                key = evt.getKeyCode();
                KeyEvent _tmp = evt;
                if (key != 10) {
                    KeyEvent _tmp1 = evt;
                    if (key != 32) {
                        break label1;
                    }
                }
                if (jTable1.getSelectedRowCount() == 1) {
                    openKlient(jTable1.getSelectedRow());
                    break label0;
                }
            }
            KeyEvent _tmp2 = evt;
            if (key == 27) {
                zakoncz();
            }
        }
    }

    private void openKlient(int nr) {
        oek = new OEKlient((Klient) kl.get(nr), mtk, false);
        oek.setVisible(true);
    }

    private void zakoncz() {
        mtw.refresh();
        dispose();
    }
    final JFileChooser fc = new JFileChooser();
    private MTKlientow mtk;
    private java.util.List kl;
    private String nazwa;
    private OEKlient oek;
    private int nrSp;
    private int nrWs;
    private MTWsi mtw;
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JLabel jLabel1;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTable jTable1;
}
